﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public partial class AxleManager
    {
        /// <summary>
        /// 基本运行参数设置
        /// </summary>
        /// <param name="axleName"></param>
        /// <param name="speed"></param>
        /// <param name="dir"></param>
        /// <param name="totalStep"></param>
        /// <returns></returns>
        public static bool ParameterSetting(
            string axleName
            , Axle.Direction dir
            , double period
            , double totalStep
         )
        {
            return ParameterSetting(axleName, dir, period, totalStep, false);
        }

        /// <summary>
        /// 基本运行参数设置
        /// </summary>
        /// <param name="axleName"></param>
        /// <param name="speed"></param>
        /// <param name="dir"></param>
        /// <param name="totalStep"></param>
        /// <returns></returns>
        public static bool ParameterSetting(
            string axleName
            , Axle.Direction dir
            , double period
            , double totalStep
            , bool force
         )
        {
            //double nStep = getRStep(dir, totalStep);
            //if (!m_rSteps.ContainsKey(axleName))
            //{
            //    m_rSteps.Add(axleName, 0.0F);
            //}
            //if (!ParameterChanged(axleName)
            //    && m_rSteps[axleName].ToString(MySetting.PositionDisDeciFormatter).
            //    Equals(nStep.ToString(MySetting.PositionDisDeciFormatter))
            //    )
            //{
            //    return true;
            //}

            if (!force && ParameterCacheManager.Compare(axleName, dir, period, totalStep)
                && !AxleCacheManager.AxleList[axleName].AxleActionStatus.VZeroIsSeted
                )
            {
                return true;
            }

            if (!IsOpen)
            {
                //setParameterChanged(axleName, false);
                return false;
            }


            string data = Protocol.Packet(
                Protocol.GetParameterSettingString(axleName, dir, period, totalStep)
                );
            if (AxleCacheManager.AxleList.ContainsKey(axleName))
            {
                AxleCacheManager.AxleList[axleName].TargetParametersSetting.Setting(dir, period, totalStep);
            }
            try
            {
                string msg = string.Empty;
                bool rst = false;
                lock (s_spLocker)
                {
                    if (spLink.CanWrite)
                    {
                        spLink.Write(data);
                        msg = spLink.ReadString(1);
                        if (string.IsNullOrEmpty(msg))
                        {
                            return false;
                        }
                        CommResultCode crcd = handleNrmAck(FunctionCode.FCodeParameterSetting, axleName, msg, false);
                        while (crcd != CommResultCode.Success)
                        {
                            if (crcd == CommResultCode.WrongFrame)
                            {
                                msg = spLink.ReadString(1);
                                if (string.IsNullOrEmpty(msg))
                                {
                                    return false;
                                }
                                crcd = handleNrmAck(FunctionCode.FCodeParameterSetting, axleName, msg, false);
                            }
                            else
                            {
                                break;
                            }
                        }
                        rst = crcd == CommResultCode.Success;
                        ;
                    }
                }

                if (rst)
                {
                    //m_rSteps[axleName] = nStep;
                    ParameterCacheManager.Update(axleName, dir, period, totalStep);

                }
                return rst;
            }
            finally
            {
                //setParameterChanged(axleName, false);
                //setIsLocked(false);
            }

        }


        /// <summary>
        /// 基本运行参数设置
        /// </summary>
        /// <param name="axleName"></param>
        /// <param name="speed"></param>
        /// <param name="dir"></param>
        /// <param name="totalStep"></param>
        /// <returns></returns>
        public static bool ParameterSettingNew(
            string axleName
            , Axle.Direction dir
            , double period
            , double totalStep
            , bool force
         )
        {

            if (!force && ParameterCacheManager.Compare(axleName, dir, period, totalStep)
                && !AxleCacheManager.AxleList[axleName].AxleActionStatus.VZeroIsSeted
                )
            {
                return true;
            }

            if (!IsOpen)
            {
                return false;
            }


            string data = Protocol.Packet(
                Protocol.GetParameterSettingString(axleName, dir, period, totalStep)
                );

            if (AxleCacheManager.AxleList.ContainsKey(axleName))
            {
                AxleCacheManager.AxleList[axleName].TargetParametersSetting.Setting(dir, period, totalStep);
            }

            return sendData(data);

            //if (rst)
            //{
            //    ParameterCacheManager.Update(axleName, dir, period, totalStep);
            //}


        }
        static void OnPMSettingRespond(ProtocolV2.UnpacketResult ur)
        {
            OnPMSettingRespond(ur.AxleName, ur.Result && ur.Action == AxleActions.ParameterSettingRespond);
        }
        static void OnPMSettingRespond(string aName,bool result)
        {
            setState(
            string.Format("参数设置({0}|[{1}]|{2}|{3})成功"
                , aName, AxleCacheManager.AxleList[aName].TargetParametersSetting.Direction.ToString()
                , AxleCacheManager.AxleList[aName].TargetParametersSetting.Period.ToString(MySetting.PositionDisDeciFormatter)
                , AxleCacheManager.AxleList[aName].TargetParametersSetting.TotalStep.ToString(MySetting.PositionDisDeciFormatter)
                )
            );
            if (result)
            {
                if (AxleCacheManager.AxleList.ContainsKey(aName))
                {
                    ParameterCacheManager.Update(aName
                        , AxleCacheManager.AxleList[aName].TargetParametersSetting.Direction
                        , AxleCacheManager.AxleList[aName].TargetParametersSetting.Period
                        , AxleCacheManager.AxleList[aName].TargetParametersSetting.TotalStep
                        );
                    AxleCacheManager.AxleList[aName].TargetParametersSetting.ResetPM();

                }

                if (AxleCacheManager.AxleList.ContainsKey(aName))
                {
                    AxleCacheManager.Update(aName);
                    if (AxleCacheManager.AxleList[aName].TargetParametersSetting.RunAfterPMSetting)
                    {
                        Run(aName);
                    }
                    AxleCacheManager.AxleList[aName].TargetParametersSetting.RunAfterPMSetting = false;
                }
            }






        }

    }
}
